package com.obradovicnjegovan.webprodavnica.components;

import java.util.Set;

import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.Block;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.tynamo.security.services.SecurityService;

import com.obradovicnjegovan.webprodavnica.controller.ControllerAL;
import com.obradovicnjegovan.webprodavnica.entities.OdabranProizvod;
import com.obradovicnjegovan.webprodavnica.pages.Catalogue;
import com.obradovicnjegovan.webprodavnica.pages.Index;

/**
 * Layout component for pages of application webprodavnica.
 */
@Import(stylesheet = {"context:layout/css/bootstrap.css", "context:layout/css/font-awesome.min.css", "context:layout/css/jquery.bxslider.css", "context:layout/css/style.css", "context:layout/css/smoothproducts.css"}, 
library = {"context:layout/js/bootstrap.js", "context:layout/js/bootstrap.touchspin.js", "context:layout/js/jquery.blImageCenter.js", "context:layout/js/jquery.bxslider.min.js", "context:layout/js/mimity.js", "context:layout/js/addthis_widget.js", "context:layout/js/smoothproducts.min.js"})
public class Layout
{
    /**
     * The page title, for the <title> element and the <h1> element.
     */
    @Property
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String title;

    @Property
    private String pageName;

    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private String sidebarTitle;

    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block sidebar;

    @Inject
    private ComponentResources resources;

    @Property
    @Inject
    @Symbol(SymbolConstants.APPLICATION_VERSION)
    private String appVersion;


    public String getClassForPageName()
    {
        return resources.getPageName().equalsIgnoreCase(pageName)
                ? "current_page_item"
                : null;
    }

    public String[] getPageNames()
    {
        return new String[]{"Index", "About", "Contact"};
    }
    
    @Inject
    private ControllerAL controllerAL;
    public int getBrojOdabranihProizvoda(){
    	int i = 0;
    	for (OdabranProizvod od : controllerAL.getKorpa().getOdabraniProizvodi()) {
			i = i + od.getKolicina();
		}
		return i;
	}
    @Property
    private OdabranProizvod op;
    public Set<OdabranProizvod> getOdabraneProizvode(){
    	return controllerAL.getKorpa().getOdabraniProizvodi();
    }
    
	public String getUkupnaCena(){
		double ukupnaCena = 0;
		try {
			for (OdabranProizvod od : controllerAL.getKorpa().getOdabraniProizvodi()) {
				ukupnaCena = ukupnaCena + (od.getProizvod().getCena()*od.getKolicina());
			}
			return  controllerAL.formatDouble(ukupnaCena);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return controllerAL.formatDouble(0);
	}
    
    public boolean getDaLiKorpaPrazna(){
    	if (controllerAL.daLiKorpaPostoji()) {
			if (controllerAL.getKorpa().getOdabraniProizvodi()==null) {
				return false;
			}
			return true;
		}
    	return false;
    }
    
    @InjectPage
    private Catalogue catalogue;
    
    public Object onPrikaziSveProizvode(){
    	catalogue.setTitle("ALL PRODUCTS");
    	catalogue.setProizvodi(controllerAL.pronadjiSveProizvode());
    	return catalogue;
    }
    
    @Inject
    @Property
    private SecurityService securityService;
    @Property
    private String username;
    @Property
    private String password;
    
    public Object onSubmitFromSigninForm(){
    	try {
    		Subject subject = securityService.getSubject();
        	UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        	subject.login(token);
        	return Index.class;
		} catch (Exception e) {
			return null;
		}
    }
    
    
    public Object onLogout(){
            Subject currentUser = securityService.getSubject();
            currentUser.logout();
            return Index.class;
    }
}
